package com.example.mypigcar.service;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.example.mypigcar.pojo.po.Car;
import com.example.mypigcar.mapper.CarDao;
import org.apache.logging.log4j.util.Strings;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;

/**
 * @author zkw
 * @Description 车辆service
 * @createTime 2021年07月02日 08:16:00
 */
@Service
public class CarServiceImpl implements CarService {
    @Resource
    CarDao carDao;

    /**
     * 添加
     */
    @Override
    public boolean save(Car car) {
        return carDao.insert(car) > 0;
    }

    /**
     * 修改
     */
    @Override
    public Car update(Car car) {
        QueryWrapper<Car> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("car_id", car.getCarId());
        carDao.update(car, queryWrapper);
        Car result = carDao.selectOne(queryWrapper);
        return result;
    }

    @Override
    public List<Car> listCars() {
        QueryWrapper<Car> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("audit", 1);
        return carDao.selectList(queryWrapper);
    }

    /**
     * 根据车牌查询车辆
     *
     * @param plate 车牌
     * @return 车辆
     */
    @Override
    public Car getCarByPlate(String plate) {
        QueryWrapper<Car> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("car_plate", plate);
        return carDao.selectOne(queryWrapper);
    }

    /**
     * 根据车牌和品牌查询车辆
     *
     * @param carPlate 车牌
     * @param carBrand 品牌
     * @return List<Car>
     */
    @Override
    public List<Car> getCarByPlateAndBrand(String carPlate, String carBrand) {
        QueryWrapper<Car> queryWrapper = new QueryWrapper<>();
        if (Strings.isEmpty(carPlate))
            queryWrapper.like("car_brand", carBrand);
        else if (Strings.isEmpty(carBrand))
            queryWrapper.eq("car_plate", carPlate);
        else
            queryWrapper.eq("car_plate", carPlate)
                    .like("car_brand", carBrand);

        List<Car> cars = carDao.selectList(queryWrapper);
        return cars;
    }
}
